<?php

class officeClassAction extends Action
{
    /**
     * 导出采购文件审批表
     * url:index.php?a=cgwjspb&m=office|index&d=cg&ajaxbool=true
     * @param $id  cg_dljg_flow 主键
     * return office文件
     */
    public function cgwjspbAjax(){
        $id = $this->request("id");
        if(empty($id)) return '数据不能为空';
        $data = m('cg_dljg_flow')->getone('id='.$id, 'proxycompany,beian_num,projectname,receivetime,lastsubtime,bugtype,proxycompanylink,proxycompanylinktel,zbzxlink,zbzxlinktel,dljgname,dljglink,dljglinktel,budget');
        if(!empty($data['receivetime'])) $data['receivetime'] = date('Y-m-d', strtotime($data['receivetime']));
        $dataRow = m('flow_log')->getall('`mid`='.$id." and `table` = 'cg_dljg_flow' and `status` = 1",'`courseid`, `checkname`, `checkid`, `optdt`, `explain`', 'optdt asc');
        $logs = array();
        foreach ($dataRow as $key => $value) {
            if(($value['courseid'] == 155) || ($value['courseid'] == 211) || ($value['courseid'] == 212)) {
                $userInfo = m('admin')->getone('id='.$value['checkid'], 'ranking');
                $ranking = $userInfo['ranking'];
            }
            if($data['budget'] >= 1000000) {
                switch ($value['courseid']) {
                    case 183://项目负责人
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr');
                        break;
                    case 181://采购科科长
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr');
                        break;
                    case 153://装备中心分管主任
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr','signature');
                        break;
                    case 154://装备中心主任
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr','signature');
                        break;
                    case 155://采购单位
                        if($ranking == '项目负责人') {
                            $logs[$key]['uid'] =  $value['checkid'];
                            $logs[$key]['type'] = array('qmimgstr','signature');
                        }
                        break;
                    default:
                        break;
                }
//            }elseif ($data['budget'] >= 500000 && $data['budget'] <= 800000) {
            }elseif ($data['budget'] < 1000000) {
                switch ($value['courseid']) {
                    case 213://项目负责人
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr');
                        break;
                    case 202://采购科科长
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr');
                        break;
                    case 204://装备中心分管主任
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr','signature');
                        break;
                    case 211://采购单位
                        if($ranking != '项目负责人') break;
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr','signature');
                        break;
                    default:
                        break;
                }
            }else {
                switch ($value['courseid']) {
                    case 214://项目负责人
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr');
                        break;
                    case 203://采购科科长
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr');
                        break;
                    case 212://采购单位
                        if($ranking != '项目负责人') break;
                        $logs[$key]['uid'] =  $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr','signature');
                        break;
                    default:
                        break;
                }
            }
        }

        c('phpOffice')->signatureWord($this->adminid, 'cg_dljg_flow', $id, $logs, 'cgwjspb');
    }

    /**
     * 导出政府采购项目抽取专家申请表
     * url:index.php?a=cqzjsqb&m=office|index&d=cg&ajaxbool=true
     * @param $id  cg_dljg_flow 主键
     * return office文件
     */
    public function cqzjsqbAjax(){
        $id = $this->request("id");
        if(empty($id)) return '数据不能为空';
        $data = m('zbcg_expertappy')->getone('id='.$id);
        $dataRow = m('flow_log')->getall('`mid`='.$id." and `table` = 'zbcg_expertappy' and `status` = 1",'courseid, step as rowNumber,name as action, checkname, optdt as time,statusname as checkStatus, checkid', 'optdt asc');
        if(empty($dataRow)) return json_encode(array('code' => 300, 'msg'=>'数据为空'));

        $logs = array();
        foreach ($dataRow as $key => $value) {
            switch ($value['courseid']) {
                case 185://项目负责人
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr');
                    break;
                case 160:// 采购科长
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr');
                    break;
                case 184:// 装备中心主任
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr');
                    break;
                default:
                    break;
            }
            unset($value['checkid']);
        }
        c('phpOffice')->signatureWord($this->adminid, 'zbcg_expertappy', $id, $logs, 'cqzjsq');
    }

    /**
     * 导出采购结果审批表表
     * url:index.php?a=cgjieguolist&m=office|index&d=cg&ajaxbool=true
     * @param $id  cg_jieguo_list 主键
     * return office文件
     */
    public function cgjieguolistAjax(){
        $id = $this->request("id");
        if(empty($id)) return '数据不能为空';
        $data = m('cg_jieguo_list')->getone('id='.$id, 'name,beian_num,budget,zbmoney,caigoutype,zbtime');
        $dataRow = m('flow_log')->getall('`mid`='.$id." and `table` = 'cg_jieguo_list' and `status` = 1",'`courseid`, `checkname`, `checkid`, `optdt`, `explain`', 'optdt asc');
        if(empty($dataRow)) return json_encode(array('code' => 300, 'msg'=>'数据为空'));
        $imgs = array();
        $logs = array();
        foreach ($dataRow as $key => $value) {
            if($value['courseid'] == 232) {
                $userInfo = m('admin')->getone('id='.$value['checkid'], 'ranking');
                $ranking = $userInfo['ranking'];
            }
            switch ($value['courseid']) {
                case 227://装备中心项目负责人审核
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr');
                    break;
                case 228://采购科长审核
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr');
                    break;
                case 229://装备中心分管副主任审核
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr');
                    break;
                case 230://装备中心主任审核
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr','signature');
                    break;
                case 232://采购单位审核
                    if($ranking == '项目负责人') {
                        $logs[$key]['uid'] = $value['checkid'];
                        $logs[$key]['type'] = array('qmimgstr', 'signature');
                    }
                    break;
                case 234://代理机构发布公示
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr','signature');
                    break;
                default:
                    break;
            }
        }

        if(empty($data)) return '数据不能为空';
        c('phpOffice')->signatureWord($this->adminid, 'cg_jieguo_list', $id, $logs, 'cgjg');
    }

    /**
     * 导出信息反馈单
     * url:index.php?a=fkd&m=office|index&d=cg&ajaxbool=true
     * @param $id  cg_dljg_flow 主键
     * return office文件
     */
    public function fkdAjax(){
        $id = $this->request("id");
        if(empty($id)) return '数据不能为空';
        $datasql = "select a.uid, a.company, a.applydt, b.name from [Q]zbcg_feedback as a inner join [Q]company as b on a.company = b.id where a.id =".$id.' order by applydt';
        $datas = $this->db->getall($datasql);
        $datas = $datas[0];
        $data = array();
        if($datas) {
            $data = array(
                'company' => $datas['name'],
                'date' => $datas['applydt'],
            );
        }
        $whereStr = "";
        $whereStr .= "a.comid = '".$datas['company']."'";
        $whereStr .= ' and a.projectadminer='.$datas['uid'];
        $whereStr .= " and d.status =1";
        $whereStr .= " and a.addtime>'".date("Y-01-01 00:00:00")."'";
        $sql = "select a.id as rowNumber,a.name,a.budget,a.proxygov,a.catalogue,a.buymode,b.name as truename from [Q]zbcg_new_info as a inner join [Q]userinfo as b on a.projectadminer=b.id inner join [Q]zbcg_new as d on a.mid = d.id where ". $whereStr;
        $dataRow = $this->db->getall($sql);
        if(empty($dataRow)) return '数据不能为空';
        $number = 0;
        foreach ($dataRow as $key => $value) {
            $number += 1;
            $dataRow[$key]['rowNumber'] = $number;
        }
        c('phpOffice')->downloadWord($data, 'fkd.docx', $dataRow);
    }

    /**
     * 导出收文单
     * url:index.php?a=swd&m=office|index&d=cg&ajaxbool=true
     * @param $id  cg_dljg_flow 主键
     * return office文件
     */
    public function swdAjax(){
        $id = $this->request("id");
        if(empty($id)) return '数据不能为空';
        $loginfo = m('flow_log')->getone('`modeid`=94 and `courseid`=112 and `mid`='.$id, 'optdt');
        $newinfo  = m('zbcg_new')->getone('`id`='.$id, 'proxycompany,symbol,projectname,note');
        $bilinfo = m('flow_bill')->getone('`modeid`=94 and `mid`='.$id, 'sericnum');
        $data = array(
            'proxycompany' => $newinfo['proxycompany'],
            'symbol' => $newinfo['symbol'],
            'projectname' => $newinfo['projectname'],
            'sericnum' => $bilinfo['sericnum'],
            'note' => $newinfo['note']
        );
        $data['acceptdate'] = '';
        if(!empty($loginfo)) $data['acceptdate'] = date('Y-m-d', strtotime($loginfo['optdt']));
        $dataRow = m('flow_log')->getall('`modeid`=94 and `mid`='.$id.' and `status` = 1', 'courseid, step as rowNumber,name as action, checkname as name, optdt as time, `explain` as checkStatus, checkid', 'optdt asc');

        $logs = array();
        foreach ($dataRow as $key => $value) {
            switch ($value['courseid']) {
                case 114://装备采购科科长
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr');
                    break;
                case 170://装备中心分管主任
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr');
                    break;
                case 171://装备中心主任
                    $logs[$key]['uid'] =  $value['checkid'];
                    $logs[$key]['type'] = array('qmimgstr');
                    break;
                default:
                    break;
            }
            unset($value['checkid']);
        }
        c('phpOffice')->signatureWord($this->adminid, 'zbcg_new', $id, $logs, 'swd', '', 2);
    }

    /**
     * 导出招标情况汇总表
     * url:index.php?a=zbqkhzb&m=office|index&d=cg&ajaxbool=true
     * @param $id
     * return excel文件
     */
    public function zbqkhzbAjax(){
        $type = $this->request("type");
        $name = $this->request("name");
        $sdate = $this->request("sdate");
        $edate = $this->request("edate");
        $ksdate = $this->request("ksdate");
        $kedate = $this->request("kedate");
        $stbudget = $this->request("stbudget");
        $enbudget = $this->request("enbudget");
        $bugtype = $this->request("bugtype");
        $comid = $this->request('comid');
        $projectadminer = $this->request("projectadminer");
        $utype = $this->request('utype'); //管理员为true   其他人员为空

        $uid = $this->adminid;
        $whereStr = "1=1";
        $approveWhere = "1=1";
        $approveWhere .= "  and d.status IN (1, 0) and d.modeid = 94";
        if ($sdate) {
            $approveWhere .= " and b.addtime > '".$sdate. " 01:01:01'";
        }
        if ($edate) {
            $approveWhere .= " and b.addtime <'". $edate. " 23:59:59'";
        }
        if ($ksdate) {
            $approveWhere .= " and b.startbiddate >= '".$ksdate. " 00:00:00'";
        }
        if ($kedate) {
            $approveWhere .= " and b.startbiddate <= '".$kedate. " 23:59:59'";
        }
        if ($stbudget) {
            $approveWhere .= " and b.budget >= ".$stbudget;
        }
        if ($enbudget) {
            $approveWhere .= " and b.budget <= ".$enbudget;
        }
        if ($bugtype) {
            $approveWhere .= " and b.buymode = '".$bugtype."'";
        }
        if ($comid) {
            $approveWhere .= " and b.comid = ".$comid;
        }
        if ($utype) {
            if ($projectadminer) $approveWhere .= " and b.projectadminer=".$projectadminer;
        } else {
            $approveWhere .= " and b.projectadminer=".$uid;
        }

        if(empty($sdate) && empty($edate)) $approveWhere .= " and b.addtime >='". date('Y'). "-01-01 00:00:00'";
        if ($type) {
            $whereStr .= " and b.type='".$type."'";
        }
        if ($name) {
            $whereStr .= " and b.name like '%".$name."%'";
        }
        $list = array();
        if ($uid) {
            $field = 'b.id, b.companyname, b.dept, b.name, b.projecticp, b.proicptime, b.type, b.buymode, b.budget, b.aftercheckmoney, b.zbmoney, b.savemoney_ys, b.savemoney_sj, b.saverate_ys, b.saverate_sj, b.proxygov, b.signupcompany, b.proclamationdate, b.startbiddate, b.wbtime, b.zbcompany, b.issignagreement, b.projectfiles, b.projectprogress, b.gathernote, a.name as projecterName, b.mergeids, b.mergename, b.no';
            $listSql = 'select '. $field ." from [Q]zbcg_project_summary as b left join [Q]admin as a on a.id = b.projectadminer inner join [Q]flow_bill as d on b.mid = d.mid where " . $whereStr .' and b.status = 1 and '. $approveWhere . ' order by b.companyname asc, b.dept asc, b.mergename';
            $list = $this->db->getall($listSql);
        }
        if(empty($list)) return '数据不能为空';
        foreach ($list as $key => $value) {
            !empty($value['mergeids']) && !empty($value['mergename']) && $list[$key]["name"] = $value["mergename"];
            !empty($value['dept']) && $list[$key]['companyname'] = $value['companyname'].'-'.$value['dept'];
            $list[$key]['budget'] = bcadd($value['budget'],0,2);
            $list[$key]['aftercheckmoney'] = bcadd($value['aftercheckmoney'],0,2);
            $list[$key]['zbmoney'] = bcadd($value['zbmoney'],0,2);
            $list[$key]['savemoney_ys'] = bcadd($value['savemoney_ys'],0,2);
            $list[$key]['savemoney_sj'] = bcadd($value['savemoney_sj'],0,2);
            unset($list[$key]['dept']);
            unset($list[$key]['mergename']);
        }
//        return json_encode($list);exit;

        if(empty($sdate) && empty($edate)) {
            $year = date('Y');
        } else {
            $syear = '';
            $eyear = '';
            !empty($sdate) && $syear = date('Y', strtotime($sdate));
            !empty($edate) && $eyear = date('Y', strtotime($edate));
            if($syear == $eyear) {
                $year = $syear;
            } else {
                if(empty($syear)) $year = $eyear;
                if(empty($eyear)) $year = $syear;
                if(!empty($syear) && !empty($eyear) && ($syear > $eyear)) $year = $eyear .'~'.$syear;
                if(!empty($syear) && !empty($eyear) && ($syear < $eyear)) $year = $syear .'~'.$eyear;
            }
        }

        $dataTmp = array('B2' => '统计日期：'.date('Y年m月d日'), 'A1' => $year.'年政府采购项目进展情况统计表');
        c('phpOffice')->ExcelsMergeCells($list, 'zbqkhzb.xlsx', $dataTmp);
    }

    /**
     * 导出内部项目审批表
     * url:index.php?a=nbxmsp&m=office|index&d=cg&ajaxbool=true
     * @param $id
     * return excel文件
     */
    public function nbxmspAjax(){
        $mid = $this->request('id');
        $unit = $this->request('unit');
        $projectname = $this->request('projectname');
        $types = $this->request('types');
        $origins = $this->request('origins');
        $stbudget = $this->request('stbudget');
        $enbudget = $this->request('enbudget');
        $natureOfFunds = $this->request('natureOfFunds');
        $projectadminer = $this->request('projectadminer');
        $detailSubject = $this->request('detailSubject');
        $catalogue = $this->request('catalogue');
        $bugtype = $this->request('bugtype');
        $company = $this->request('company');
        $projectNameFund = $this->request('projectNameFund');
        $where = '1=1';
        if ($mid) $where .= " and a.mid=".$mid;
        if ($unit) $where .= " and a.companyname='".$unit.'\'';
        if ($projectname) $where .= " and a.name='".$projectname.'\'';
        if ($types) $where .= " and a.type='".$types.'\'';
        if ($origins) $where .= " and a.producing_area='".$origins.'\'';
        if ($stbudget) $where .= " and a.budget>=".$stbudget;
        if ($enbudget) $where .= " and a.budget<=".$enbudget;
        if ($natureOfFunds) $where .= " and a.funds_type='".$natureOfFunds.'\'';
        if ($projectadminer) $where .= " and a.projectadminer=".$projectadminer;
        if ($detailSubject) $where .= " and a.detail='".$detailSubject.'\'';
        if ($catalogue) $where .= " and a.catalogue='".$catalogue.'\'';
        if ($bugtype) $where .= " and a.buymode='".$bugtype.'\'';
        if ($company) $where .= " and a.proxycompany='".$company.'\'';
        if ($projectNameFund) $where .= " and a.budget_item_name='".$projectNameFund.'\'';
        $baseSql = "select a.type from [Q]zbcg_approve_sub  as a left join [Q]admin as b on a.projectadminer = b.id where ". $where ." group by a.type";
        $data = $this->db->getall($baseSql);
        if(empty($data)) return '数据不能为空';
        foreach ($data as $key => $value) {
            $field = 'a.id, a.companyname, a.dept, a.name, a.type, a.producing_area, a.budget, a.funds_type, a.budget_item_name, a.detail, a.catalogue, a.buymode, a.proxycompany, b.name as projecterName, a.note, a.mergepeojectids';
            $sql = 'select '. $field ." from [Q]zbcg_approve_sub as a left join [Q]admin as b on a.projectadminer = b.id where " .$where. " and a.type = '". $value['type'] ."' and a.name != '' order by mergepeojectids desc ,companyname asc";
            $list = $this->db->getall($sql);
            if(empty($list)) continue;
            $num = 0;
            for ($i = 0; $i < count($list); $i++) {
                if($list[$i]['dept']) $list[$i]['companyname'] = $list[$i]['companyname'].'-'.$list[$i]['dept'];
                $mergepeojectids = $list[$i]["mergepeojectids"];
                if(!empty($mergepeojectids)) {
                    $subinfoId = $list[$i]["id"];
                    $sub2proEntry = $this->db->getone("[Q]zbcg_approvesub2mergepro", "subinfoid=".$subinfoId);
                    if ($sub2proEntry) {
                        $merge_proid = $sub2proEntry["merge_proid"];
                        $mergeProjectInfo = m("zbcg_merge_project")->getone("id=".$merge_proid);
                        $list[$i]["name"] = $mergeProjectInfo["projectname"];
                    }
                }
//                $num += 1;
//                $list[$i]['id'] = $num;
                //处理表格合并时不能销毁mergepeojectids
//                unset($list[$i]["mergepeojectids"]);
                unset($list[$i]["dept"]);
            }
            $data[$key]['data'] = $list;
        }
//        echo json_encode($data);
//        exit;
        //计算委托单年限和第几批
        $date  = m('zbcg_approve')->getone('id='.$mid, 'applydt');
        $date = date('Y', strtotime($date['applydt']));
        $lists = m('zbcg_approve')->getall('applydt>=\''.$date.'-01-01\''.' && '.'applydt<=\''.$date.'-12-31\'', 'id', 'id asc');
        $num = '一';
        foreach($lists as $key => $value){
            if($value['id'] == $mid) {
                $num = m('turn')->numTurnChinese($key);
            }
        }
        $dataTmp = array('A1' => $date .'年集中采购项目第'. $num .'批委托单');
        c('phpOffice')->downloadExcels($data, 'nbxmsp.xlsx', $dataTmp);
    }

    /**
     * 导出采购审批表
     * url:index.php?a=cgsq&m=office|index&d=cg&ajaxbool=true
     * @param $id
     * return excel文件
     */
    public function cgsqAjax()
    {
        $id = $this->request("id");
        $new = m('zbcg_new')->getone('id='.$id, 'proxycompany, company_address, companyadminer, processer, linktel');
        $data = array();
        isset($new['proxycompany']) && $data['companyName'] = $new['proxycompany'];
        isset($new['company_address']) && $data['address'] = $new['company_address'];
        isset($new['processer']) && $data['agent'] = $new['processer'];
        isset($new['linktel']) && $data['mobile'] = $new['linktel'];
        $dataRow = m('zbcg_new_info')->getrows('mid = '. $id, 'dept, name, type, producing_area, budget, budget_item_name, funds_type, detail, other', 'id asc');
        if(empty($dataRow)) return '数据不能为空';
        $total = 0;
        $number = 0;
        foreach ($dataRow as $key => $value) {
            $number += 1;
            $total += $value['budget'];
            $dataRow[$key]['rowNumber'] = $number;
            $dataRow[$key]['dept'] = $new['proxycompany'];
            if(!empty($value['dept'])) {
                $dataRow[$key]['dept'] = $new['proxycompany'].'-'.$value['dept'];
            }
        }
        $data['total'] = $total;

        $logList = m('flow_log')->getall('`mid`='.$id." and `table` = 'zbcg_new' and `status` = 1",'checkid, courseid', 'optdt asc');
        $logs = array();
        foreach ($logList as $k => $v) {
            if ($v['courseid'] == 112) {
                $logs[$k]['uid'] = $v['checkid'];
                $logs[$k]['type'] = array('qmimgstr');
            }
        }

        c('phpOffice')->signatureWord($this->adminid, 'zbcg_new', $id, $logs, 'cgsq', true);
    }

    /**
     * 导出采购招标计划表
     * url:index.php?a=cgzbjhb&m=office|index&d=cg&ajaxbool=true
     * @param $id
     * return excel文件
     * @Auther: lv
     * @Email: 578530370@qq.com
     * @Date: 2020/3/27 15:42
     */
    public function cgzbjhbAjax()
    {
        $type = $this->request("type");
        $name = $this->request("name");
        $sdate = $this->request("sdate");
        $edate = $this->request("edate");
        $ksdate = $this->request("ksdate");
        $kedate = $this->request("kedate");
        $stbudget = $this->request("stbudget");
        $enbudget = $this->request("enbudget");
        $bugtype = $this->request("bugtype");
        $comid = $this->request('comid');
        $projectadminer = $this->request("projectadminer");
        $utype = $this->request('utype'); //管理员为真   其他人员为空

        $uid = $this->adminid;
        $whereStr = "1=1";
        $approveWhere = "1=1";
        if ($sdate) {
            $approveWhere .= " and b.addtime > '".$sdate. " 01:01:01'";
        }
        if ($edate) {
            $approveWhere .= " and b.addtime <'". $edate. " 23:59:59'";
        }
        if ($ksdate) {
            $approveWhere .= " and b.startbiddate >= '".$ksdate. "'";
        }
        if ($kedate) {
            $approveWhere .= " and b.startbiddate <= '".$kedate."'";
        }
        if(empty($ksdate) && empty($kedate)) {
            $ksdate = date('Y-m-d', strtotime('next week'));
            $kedate = date('Y-m-d',strtotime('next week')+(86400*6));
            $approveWhere .= " and b.startbiddate >= '".$ksdate."'";
            $approveWhere .= " and b.startbiddate <= '".$kedate."'";
        }
        if ($stbudget) {
            $approveWhere .= " and b.budget >= ".$stbudget;
        }
        if ($enbudget) {
            $approveWhere .= " and b.budget <= ".$enbudget;
        }
        if ($bugtype) {
            $approveWhere .= " and b.buymode = '".$bugtype."'";
        }
        if ($comid) {
            $approveWhere .= " and b.comid = ".$comid;
        }
        if ($utype) {
            if ($projectadminer) $approveWhere .= " and b.projectadminer=".$projectadminer;
        } else {
            $approveWhere .= " and b.projectadminer=".$uid;
        }
        if(empty($sdate) && empty($edate)) $approveWhere .= " and b.addtime >='". date('Y'). "-01-01 00:00:00'";
        if ($type) {
            $whereStr .= " and b.type='".$type."'";
        }
        if ($name) {
            $whereStr .= " and b.name like '%".$name."%'";
        }

        $list = array();
        if ($uid) {
            $field = 'b.id, b.companyname, b.startbiddate, b.name, b.proxygov, b.budget, a.dljglink, a.zbzxlink, b.buymode, b.buytype';
            $listSql = 'select '. $field ." from [Q]zbcg_project_summary as b left join [Q]cg_dljg_flow as a on a.projectnum = b.id where " . $whereStr . ' and ' . $approveWhere. ' order by b.startbiddate';
//			echo $listSql;exit();
            $list = $this->db->getall($listSql);
        }
        if(empty($list)) return '数据不能为空';
        $number = 0;
        foreach ($list as $key => $value) {
            $number += 1;
            $list[$key]['id'] = $number;

            $list[$key]['budget'] = bcadd($value['budget'],0,2);
        }

        $dataTmp = array('A2' => '时间：'.date('m月d日',strtotime($ksdate)).'-'.date('m月d日',strtotime($kedate)));
        c('phpOffice')->downloadExcel($list, 'cgzbjhb.xlsx', $dataTmp);
    }

    /**
     *导出采购项目委托计划汇总表
     *  url:index.php?a=wtjhhzb&m=office|index&d=cg&ajaxbool=true
     * @Auther: lv
     * @Email: 578530370@qq.com
     * @Date: 2020/4/9 17:29
     */
    public function wtjhhzbAjax()
    {
        $sdate = $this->request('sdate');
        $edate = $this->request('edate');
        $utype = $this->request('utype');
        $projectadminer = $this->request('projectadminer');
        $where = "1=1";
        $where .= "  and c.status = 1 and c.modeid = 94";
        if ($sdate) {
            $where .= " and c.updt >= '".$sdate. " 00:00:01'";
        }
        if ($edate) {
            $where .= " and c.updt <='". $edate. " 23:59:59'";
        }
        if(empty($sdate) && empty($edate)) $where .= " and c.updt >='". date('Y'). "-01-01 00:00:00'";
        if($utype) {
            if ($projectadminer) $where .= " and b.projectadminer=".$projectadminer;
        }else{
            $where .= ' and b.projectadminer = '.$this->adminid;
        }
        $sql = "select group_concat(b.id) as ids from xinhu_zbcg_approve_sub as a inner join xinhu_zbcg_new_info as b on a.projectid = b.id inner join xinhu_flow_bill as c on b.mid = c.mid where " .$where. " GROUP BY a.mid ORDER BY c.updt";
        $lists = $this->db->getall($sql);
        $total1 = 0;
        $total2 = 0;
        $total3 = 0;
        $total4 = 0;
        $arr = array('工程类', '服务类', '电子卖场', '货物类');
        foreach ($lists as $key => $value) {
            $data = $this->db->getall("select type, sum(budget) as budget from xinhu_zbcg_new_info where id in (". $value['ids'] .") group by type");
//            $lists[$key]['name'] = '第'.m('turn')->numTurnChinese($key).'批';
            unset($lists[$key]['ids']);
            $dataArr['name'] = '第'.m('turn')->numTurnChinese($key).'批';
            $dataArr['工程类'] = 0;
            $dataArr['服务类'] = 0;
            $dataArr['电子卖场'] = 0;
            $dataArr['货物类'] = 0;
            for ($i = 0; $i < count($data); $i++) {
                $dataArr[$data[$i]['type']] = $data[$i]['budget'];
            }
            $lists[$key] = $dataArr;
            //处理合计
            foreach ($data as $k => $v) {
                switch ($v['type']) {
                    case '工程类':
                        $total1 += $v['budget'];
                        break;
                    case '服务类':
                        $total2 += $v['budget'];
                        break;
                    case '电子卖场':
                        $total3 += $v['budget'];
                        break;
                    case '货物类':
                        $total4 += $v['budget'];
                        break;
                }
            }
        }
        $lists[] = array(
                'name' => '合计',
                '工程类' => $total1,
                '服务类' => $total2,
                '电子卖场' => $total3,
                '货物类' => $total4
        );
        $str = '';
        if(!empty($sdate) && !empty($edate)) {
            $str1 = date('Y年m', strtotime($sdate));
            $str2 = date('-m月', strtotime($edate));
            $str = $str1.$str2;
        }
        $dataTmp = array('A2' => '制表时间：'.date('Y年m月d日'), 'A1' => $str.'政府集中采购项目委托计划汇总表');
        c('phpOffice')->downloadExcel($lists, 'wtjhhzb.xlsx', $dataTmp, array('first' => 'B', 'second' => 'E'));
    }

    /**
     * 导出采购项目委托计划明细表
     * url:index.php?a=wtjhmxb&m=office|index&d=cg&ajaxbool=true
     * @Auther: lv
     * @Email: 578530370@qq.com
     * @Date: 2020/4/9 10:00
     */
    public function wtjhmxbAjax(){
        $uid = $this->adminid;
        $whereStr = "1=1";

        $type = $this->request("type");
        $name = $this->request("name");
        $sdate = $this->request("sdate");
        $edate = $this->request("edate");
        $stbudget = $this->request("stbudget");
        $enbudget = $this->request("enbudget");
        $bugtype = $this->request("bugtype");
        $comid = $this->request('comid');
        $utype = $this->request('utype');
        $projectadminer = $this->request("projectadminer");

        $approveWhere = "1=1";
        $approveWhere .= "  and d.status = 1 and d.modeid = 94";

        if ($sdate) {
            $approveWhere .= " and d.updt >= '".$sdate. " 00:00:01'";
        }
        if ($edate) {
            $approveWhere .= " and d.updt <='". $edate. " 23:59:59'";
        }
        if ($stbudget) {
            $approveWhere .= " and b.budget >= ".$stbudget;
        }
        if ($enbudget) {
            $approveWhere .= " and b.budget <= ".$enbudget;
        }
        if ($bugtype) {
            $approveWhere .= " and b.buymode = '".$bugtype."'";
        }
        if ($comid) {
            $approveWhere .= " and b.comid = ".$comid;
        }
        if ($projectadminer) {
            if ($projectadminer) $approveWhere .= " and b.projectadminer=".$projectadminer;
        }

        if(empty($sdate) && empty($edate)) $approveWhere .= " and c.updt >='". date('Y'). "-01-01 00:00:00'";

        if ($type) {
            $whereStr .= " and b.type='".$type."'";
        }
        if ($name) {
            $whereStr .= " and b.name like '%".$name."%'";
        }
        if($utype) {
            if ($projectadminer) $whereStr .= " and b.projectadminer=".$projectadminer;
        }else{
            $whereStr .= ' and b.projectadminer = '.$this->adminid;
        }
        if ($uid) {
            $listSql = "select b.id,c.name as companyname,b.budget,b.funds_type,b.name,b.detail,b.type,b.catalogue,b.buymode,b.gathernote,b.proxygov,a.name as projecterName from [Q]zbcg_new_info as b left join [Q]company as c on b.comid=c.id left join [Q]admin as a on a.id = b.projectadminer inner join [Q]flow_bill as d on b.mid = d.mid where " . $whereStr .' and '. $approveWhere;
//            echo $listSql;exit;
            $list = $this->db->getall($listSql);
        }
        $num = 0;
        foreach ($list as $key => $value){
            $num += 1;
            $list[$key]['id'] = $num;
        }
        c('phpOffice')->downloadExcel($list, 'wtjhmxb.xlsx');
    }
}